//
//  RootViewController.m
//  新浪微博OC
//
//  Created by  justinchou on 16/2/23.
//  Copyright © 2016年  justinchou. All rights reserved.
//

#import "RootViewController.h"
#import "VisitorView.h"
#import "WebViewController.h"

@interface RootViewController () <UITableViewDataSource, UITableViewDelegate, VisitorViewDelegate>

@property (nonatomic) BOOL isLogin;

@end

@implementation RootViewController


#pragma mark - 懒加载
-(VisitorView *)visitorView {
    if (_visitorView == nil) {
        _visitorView = [[VisitorView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    }
    return _visitorView;
}

- (UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] init];
    }
    return _tableView;
}

- (UIRefreshControl *)refreshControl{
    if (_refreshControl == nil) {
        _refreshControl = [[UIRefreshControl alloc] init];
    }
    return _refreshControl;
}
/// 懒加载上拉刷新控件并设置属性
///
/// @return 上拉刷新控件
- (UIActivityIndicatorView *)pullupView{
    if (_pullupView == nil) {
        _pullupView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        _pullupView.color = [UIColor purpleColor];
        
    }
    return _pullupView;
}

#pragma mark - 实现代理方法
- (void)visitorViewDidLogin{
    NSLog(@"点击登录");
    WebViewController *webView = [[WebViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:webView];
    
    [self presentViewController:nav animated:YES completion:nil];
}

- (void)visitorViewDidRegister{
    NSLog(@"点击注册！");
    
}

#pragma mark - 生命周期
- (void)loadView{
    self.isLogin = false;
    
    //使用isLogin来判断加载哪一个view
    self.isLogin ? [self setupTableView] : [self setupVisitorView];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    if (self.isLogin) {
        [self loadData];
    }
    
    
}

- (void)loadData {
    NSLog(@"改变了");
}


#pragma mark - 设置访客视图
- (void) setupVisitorView {
    self.view = self.visitorView;
    
    self.visitorView.delegate = self;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"注册" style:UIBarButtonItemStylePlain target:self action:@selector(visitorViewDidRegister)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"登录" style:UIBarButtonItemStylePlain target:self action:@selector(visitorViewDidLogin)];
}

#pragma mark - 设置表单视图
- (void) setupTableView {
    self.view = self.tableView;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    self.navigationItem.leftBarButtonItem = nil;
    self.navigationItem.rightBarButtonItem = nil;
}

#pragma mark - 设置刷新控件
- (void)setupRefreshControl {
    //设置下拉刷新
    self.refreshControl.tintColor = [UIColor purpleColor];
    [self.tableView addSubview:self.refreshControl];
    [self.refreshControl addTarget:self action:@selector(loadData) forControlEvents:UIControlEventValueChanged];
    //设置上拉刷新
    self.tableView.tableFooterView = self.pullupView;
}

#pragma mark - 数据源以及代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [[UITableViewCell alloc] init];
}

@end
